package com.nit.service.impl;

import com.nit.dto.MemberDTO;
import com.nit.mapper.MemberMapper;
import com.nit.service.MemberService;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper  memberMapper;

    @Override
    public List<MemberDTO> query(String memberName) throws Exception {

        List<MemberDTO> memberDTOS = new ArrayList<>();

        memberDTOS = memberMapper.query(memberName);

        return memberDTOS;
    }

    /**
    * @Description: 删除禁用
    * @Author: Lc
    * @Date: 2019/9/9
    */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long remove(String memberIds) throws Exception {
        JSONArray jsonArray = JSONArray.fromObject(memberIds);
        for (int i = 0; i < jsonArray.size(); i++) {
            memberMapper.remove(Long.parseLong(jsonArray.get(i).toString()));
        }
        return Long.parseLong(String.valueOf(jsonArray.size()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long change(String memberIds) throws Exception {
        JSONArray jsonArray = JSONArray.fromObject(memberIds);
        for (int i = 0; i < jsonArray.size(); i++) {
            memberMapper.change(Long.parseLong(jsonArray.get(i).toString()));
        }
        return Long.parseLong(String.valueOf(jsonArray.size()));
    }
}
